package reflect;

import reflect.annotations.AutoRunClass;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;

/**
 * 扫描当前类所在的同一个包的所有类，并实例化那些被@AutoRunClass注解标注的类
 */
public class Test4 {
    public static void main(String[] args) throws Exception {
        Class cls = Test4.class;
        String packname = cls.getPackage().getName();
        File dir = new File(Test3.class.getResource(".").toURI());
        File[] subs = dir.listFiles(file -> file.getName().endsWith(".class"));
        for (File sub : subs) {
            String filename = sub.getName();
            String classname = filename.substring(0, filename.indexOf("."));
            classname = packname + "." + classname;
            Class c = Class.forName(classname);
            if (c.isAnnotationPresent(AutoRunClass.class)) {
                System.out.println("实例化:"+classname);
                Object o = c.newInstance();
                System.out.println(o);
            }
        }
    }
}
